package com.youboy.oauth.signature;

import java.io.UnsupportedEncodingException;

import javax.crypto.spec.SecretKeySpec;

import com.youboy.oauth.signature.method.HMAC_SHA1SignatureMethod;

/**
 * 
 * @author loudyn
 * 
 */
public class CoreOAuthSignatureFactory implements OAuthSignatureFactory {
	private boolean supportHMAC_SHA1 = true;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.youboy.oauth.signature.OAuthSignatureFactory#getSignatureMethod(java.lang.String, java.lang.String)
	 */
	@Override
	public OAuthSignatureMethod getSignatureMethod(String signatureMethod, String signatureSecret)
														throws UnsupportedSignatureMethodException {

		if (isSupportHMAC_SHA1() && HMAC_SHA1SignatureMethod.SIGNATURE_NAME.equals(signatureMethod)) {

			try {

				byte[] key = signatureSecret.getBytes("UTF-8");
				SecretKeySpec keySpec = new SecretKeySpec(key, HMAC_SHA1SignatureMethod.MAC_NAME);
				return new HMAC_SHA1SignatureMethod(keySpec);
			} catch (UnsupportedEncodingException e) {
				throw new RuntimeException(e);
			}
		}

		throw new UnsupportedSignatureMethodException(String.format("%s signatureMethod is unsupported", signatureMethod));
	}

	public boolean isSupportHMAC_SHA1() {
		return supportHMAC_SHA1;
	}

	public void setSupportHMAC_SHA1(boolean supportHMAC_SHA1) {
		this.supportHMAC_SHA1 = supportHMAC_SHA1;
	}
}
